/**
	@file achd-soc.h
	@author Andrew D. Zonenberg
	@brief Main include file
 */
#ifndef ACHDSOC_H
#define ACHDSOC_H

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Coprocessor 0 registers
// No selector fields unlike MIPS32
#define CP0_COUNT                             $9

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Mapping glue

//need to make sure main() is in .startup for now
void main(void) __attribute__ ((section(".startup")));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UART

extern volatile unsigned int U1TXBUF __attribute__((section(".sfrs")));

extern volatile unsigned int U1RXBUF __attribute__((section(".sfrs")));

extern volatile unsigned int U1BRG __attribute__((section(".sfrs")));

extern volatile unsigned int U1STAT __attribute__((section(".sfrs")));

typedef union
{
	struct
	{
		unsigned TXERR:1;				//Set in case of a transmit error (should not be possible).
		unsigned TXACTIVE:1;			//Set when a byte is being transmitted.
		unsigned TXFULL:1;				//Set when transmit fifo is full.
		unsigned TXEMPTY:1;				//Set when transmit fifo is empty.
		unsigned TXUNDERFLOW:1;			//Set if transmit fifo underflowed (should not be possible). Must be manually cleared.
		unsigned TXOVERFLOW:1;			//Set if transmit fifo overflowed. Must be manually cleared.
		unsigned :2;					//padding
		
		unsigned RXACTIVE:1;			//Set when a byte is being received.
		unsigned RXFULL:1;				//Set when rx fifo is full.
		unsigned RXEMPTY:1;				//Set when rx fifo is empty.
		unsigned RXUNDERFLOW:1;			//Set if rx fifo underflowed. Must be manually cleared.
		unsigned RXOVERFLOW:1;			//Set if rx fifo overflowed. Must be manually cleared.
		unsigned:3;						//padding
		
		unsigned:8;						//padding
		unsigned:8;						//padding
	};
} _UARTSTATbits_t;
extern volatile _UARTSTATbits_t U1STATbits __asm__ ("U1STAT") __attribute__((section(".sfrs")));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LED bank

//32-bit register but only low-order 8 bits are set. MSB is leftmost LED on the dev board.
extern volatile unsigned int LEDBANKA __attribute__((section(".sfrs")));
extern volatile unsigned int LEDBANKASET __attribute__((section(".sfrs")));
extern volatile unsigned int LEDBANKACLR __attribute__((section(".sfrs")));
extern volatile unsigned int LEDBANKAINV __attribute__((section(".sfrs")));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Button array

extern volatile unsigned int BTNARRAY __attribute__((section(".sfrs")));

typedef union
{
	struct
	{
		unsigned btn0:1;
		unsigned btn1:1;
		unsigned btn2:1;
		unsigned btn3:1;
		unsigned :4;					//padding
		
		unsigned:8;						//padding
		
		unsigned:8;						//padding
		unsigned:8;						//padding
	};
} _BTNARRAYbits_t;
extern volatile _BTNARRAYbits_t BTNARRAYbits __asm__ ("BTNARRAY") __attribute__((section(".sfrs")));


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Flash controller

#endif
